package com.ruoyi.web.controller.examine.buffet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Buffet;
import com.ruoyi.common.core.domain.entity.BuffetExamine;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.BuffetExamineService;
import com.ruoyi.system.service.BuffetService;
import com.ruoyi.system.service.resp.BuffetExamineDTO;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/examine/goods/buffet")
public class BuffetExamineController extends BaseController {
    @Resource
    private BuffetExamineService buffetExamineService;

    @Resource
    private BuffetService buffetService;

    @Log(title = "自助餐质检", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(BuffetExamine examinePlan)
    {
        startPage();
        List<BuffetExamine> list = buffetExamineService.selectList(examinePlan);
        return getDataTable(fillToDTO(list));
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success((buffetExamineService.selectById(id)));
    }

    @Log(title = "自助餐质检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BuffetExamine examinePlan) {
        examinePlan.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = buffetExamineService.update(examinePlan);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "自助餐质检", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(buffetExamineService.deleteById(id));
    }


    private List<BuffetExamineDTO> fillToDTO(List<BuffetExamine> buffetExamines) {
        PageInfo<BuffetExamine> oldPage = new PageInfo<>(buffetExamines);
        Page<BuffetExamineDTO> newPage = new Page<>();
        newPage.setTotal(oldPage.getTotal());
        if (CollectionUtils.isEmpty(buffetExamines)) {
            return newPage;
        }
        List<Long> buffetIds = buffetExamines.stream().map(BuffetExamine::getBuffetId).collect(Collectors.toList());
        List<Buffet> buffets = buffetService.selectByIds(buffetIds);
        Map<Long, Buffet> buffetMap;
        if (!CollectionUtils.isEmpty(buffets)) {
            buffetMap = buffets.stream().collect(Collectors.toMap(Buffet::getId, b -> b));
        } else {
            buffetMap = new HashMap<>();
        }
        List<BuffetExamineDTO> buffetExamineDTOS = buffetExamines.stream().map(b -> {
            BuffetExamineDTO buffetExamineDTO = new BuffetExamineDTO();
            buffetExamineDTO.setId(b.getId());
            buffetExamineDTO.setBuffetId(b.getBuffetId());
            buffetExamineDTO.setBuffetName(Optional.ofNullable(buffetMap.get(b.getBuffetId())).map(Buffet::getName).orElse(null));
            buffetExamineDTO.setStatus(b.getStatus());
            buffetExamineDTO.setInspector(b.getInspector());
            buffetExamineDTO.setCheckTime(b.getCheckTime());
            buffetExamineDTO.setCreateBy(b.getCreateBy());
            buffetExamineDTO.setCreateTime(b.getCreateTime());
            buffetExamineDTO.setUpdateBy(b.getUpdateBy());
            buffetExamineDTO.setUpdateTime(b.getUpdateTime());
            buffetExamineDTO.setRemark(b.getRemark());
            buffetExamineDTO.setArea(b.getArea());
            return buffetExamineDTO;
        }).collect(Collectors.toList());
        newPage.addAll(buffetExamineDTOS);
        return newPage;
    }
}
